package com.wnd.dell.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.duowan.mobile.netroid.NetroidError;
import com.google.gson.Gson;
import com.viewpagerindicator.TabPageIndicator;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrClassicDefaultFooter;
import in.srain.cube.views.ptr.PtrClassicDefaultHeader;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.util.PtrLocalDisplay;

public class MainActivity1 extends Activity implements View.OnClickListener{
    private View view1, view2, view3, view4;
    private ViewPager viewPager;
    private List<View> viewList;
    private TabPageIndicator tabPageIndicator;
    private MyAdapter listViewWeatherAdapter;
    private TrainAdapter listViewTrainAdapter;
    private FoodAdapter listViewFoodAdapter;
    private BookAdapter listViewBookAdapter;
    private EditText trainEdit;
    private Button searchBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);

        tabPageIndicator = (TabPageIndicator) findViewById(R.id.viewpager_indicator);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        LayoutInflater inflater = getLayoutInflater();
        view1 = inflater.inflate(R.layout.layout1, null);
        view2 = inflater.inflate(R.layout.layout2, null);
        view3 = inflater.inflate(R.layout.layout3, null);
        view4 = inflater.inflate(R.layout.layout4, null);



        final PtrFrameLayout ptrFrameLayout =(PtrFrameLayout) view1.findViewById(R.id.store_house_ptr_frame) ;

        ptrFrameLayout.setMode(PtrFrameLayout.Mode.BOTH);


        PtrClassicDefaultHeader header =new  PtrClassicDefaultHeader(this);
        PtrClassicDefaultFooter footer =new PtrClassicDefaultFooter(this);
        ptrFrameLayout.setHeaderView(header);
        ptrFrameLayout.addPtrUIHandler(header );

        ptrFrameLayout.setFooterView(footer);
        ptrFrameLayout.addPtrUIHandler(footer);


        ptrFrameLayout.setPtrHandler(new PtrDefaultHandler2() {
            //加载更多
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                frame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getWeather();
                        ptrFrameLayout.refreshComplete();
                    }
                }, 1800);
            }

            //上拉刷新
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                frame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getWeather();
                        ptrFrameLayout.refreshComplete();
                    }
                }, 1800);
            }
        });



        listViewWeatherAdapter = new MyAdapter(this);
        final ListView listView = (ListView) view1.findViewById(R.id.weather_listView);
        listView.setAdapter(listViewWeatherAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    Intent intent = new Intent(MainActivity1.this, MainActivity2.class);
                    intent.putExtra("wode","canshu");
                    startActivity(intent);
                }
            }
        });


        listViewTrainAdapter =new TrainAdapter(this);
        ListView listView2 = (ListView) view2.findViewById(R.id.train_listView);
        listView2.setAdapter(listViewTrainAdapter);

        trainEdit =(EditText) view2.findViewById(R.id.et_name);
        searchBtn =(Button) view2.findViewById(R.id.btn_search);
        searchBtn.setOnClickListener(this);

        listViewFoodAdapter =new FoodAdapter(this);
        ListView listView3 = (ListView) view3.findViewById(R.id.food_listView);
        listView3.setAdapter(listViewFoodAdapter);


        listViewBookAdapter = new BookAdapter(this);
        ListView listView4 = (ListView) view4.findViewById(R.id.book_listView);
        listView4.setAdapter(listViewBookAdapter);


        viewList = new ArrayList<>();
        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);
        viewList.add(view4);

        PagerAdapter pagerAdapter = new PageAdapter(viewList);
        viewPager.setAdapter(pagerAdapter);
        tabPageIndicator.setViewPager(viewPager);
        tabPageIndicator.setBackgroundColor(Color.rgb(105, 139, 105));


        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                int position1 = position + 1;
                Toast.makeText(getApplicationContext(), "这是第" + position1 + "页", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        Netroid.getFood(new Netroid.NetroidCallBack() {
            @Override
            public void onSuccess(String response) {
                Gson gson = new Gson();
                listViewFoodAdapter.setData(gson.fromJson(response,FoodDate.class));

            }

            @Override
            public void onFail(NetroidError error) {
                Log.e("error",error.getMessage());

            }
        });
        Netroid.getbook(new Netroid.NetroidCallBack() {
            @Override
            public void onSuccess(String response) {
                Gson gson = new Gson();
                listViewBookAdapter.setData(gson.fromJson(response,BookDate.class));

            }

            @Override
            public void onFail(NetroidError error) {
                Log.e("error",error.getMessage());

            }
        });
//        getWeather();

    }


    private void getWeather()
    {
        Netroid.getWeather(new Netroid.NetroidCallBack() {
            @Override
            public void onSuccess(String response) {
                Gson gson = new Gson();
                listViewWeatherAdapter.setData(gson.fromJson(response, WeatherData.class));

            }

            @Override
            public void onFail(NetroidError error) {
                Log.e("error",error.getMessage());

            }
        });

    }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.btn_search:
                Netroid.getTrain(trainEdit.getText().toString(),"2fe5c2efa26515634ea11e3e7c27dd6f",new Netroid.NetroidCallBack() {
                    @Override
                    public void onSuccess(String response) {
                        Gson gson = new Gson();
                        listViewTrainAdapter.setData(gson.fromJson(response,TrainDate.class));
                    }

                    @Override
                    public void onFail(NetroidError error) {
                        Log.e("error",error.getMessage());
                    }
                });


                break;
        }
    }


    @Override
    protected void onStart() {
        super.onStart();
    }



    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

